/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = frame_params.c                                     */
/*  NOTE      =                                                    */
/*  DATE      = 2014/11/17 by yaohl                                */
/*******************************************************************/

#include <stdio.h>

#include "frcomm.h"
#include "frlog.h"
#include "frconfig.h"
#include "frmem.h"

#include "frame_params.h"

Frapm_params st_apm_params;

/*******************************************************************************
*  FUNC     :  获取配置信息
*  ARGS     :
*  RTN      :  FR_SUCC:成功, FR_FAIL:失败
*  NOTE     :
*******************************************************************************/
int frame_params_get(void)
{
    FILE *fp = NULL;
    int i_ret_fun=0, i_value=0;
    char *p_key=NULL, *p_value=NULL;

    fp = frconfig_open(APM_CFG_FILENAME_APM);
    if (fp == NULL)
    {
        FRLOG_ERROR_DETAIL("config file err[%s]", APM_CFG_FILENAME_APM);
        return FR_FAIL;
    }

    p_key = "apm_ct_hash_size";
    i_ret_fun = frconfig_int(fp, p_key, &i_value);
    if(i_ret_fun == APM_FRCONFIG_SUCC)
        st_apm_params.ui_ct_hash_size = i_value;
    else
    {
        FRLOG_INFO_DETAIL("[%s] key[%s] err", APM_CFG_FILENAME_APM, p_key);
        st_apm_params.ui_ct_hash_size = 65536;
    }

    p_key = "apm_ct_max_cnt";
    i_ret_fun = frconfig_int(fp, p_key, &i_value);
    if(i_ret_fun == APM_FRCONFIG_SUCC)
        st_apm_params.ui_ct_max_cnt = i_value;
    else
    {
        FRLOG_INFO_DETAIL("[%s] key[%s] err", APM_CFG_FILENAME_APM, p_key);
        st_apm_params.ui_ct_max_cnt = 1000000;
    }

    p_key = "apm_ct_data_pkg";
    i_ret_fun = frconfig_int(fp, p_key, &i_value);
    if(i_ret_fun == APM_FRCONFIG_SUCC)
        st_apm_params.ui_ct_data_pkgs = i_value;
    else
    {
        FRLOG_INFO_DETAIL("[%s] key[%s] err", APM_CFG_FILENAME_APM, p_key);
        st_apm_params.ui_ct_data_pkgs = 1000000;
    }

    p_key = "apm_tcp_est_timeout";
    i_ret_fun = frconfig_int(fp, p_key, &i_value);
    if(i_ret_fun == APM_FRCONFIG_SUCC)
        st_apm_params.ui_tcp_est_timeout = i_value;
    else
    {
        FRLOG_INFO_DETAIL("[%s] key[%s] err", APM_CFG_FILENAME_APM, p_key);
        st_apm_params.ui_tcp_est_timeout = 1080; //60*18 sec
    }

    p_key = "apm_tcp_syn_timeout";
    i_ret_fun = frconfig_int(fp, p_key, &i_value);
    if(i_ret_fun == APM_FRCONFIG_SUCC)
        st_apm_params.ui_tcp_syn_timeout = i_value;
    else
    {
        FRLOG_INFO_DETAIL("[%s] key[%s] err", APM_CFG_FILENAME_APM, p_key);
        st_apm_params.ui_tcp_syn_timeout = 30; //30 sec
    }

    p_key = "apm_udp_est_timeout";
    i_ret_fun = frconfig_int(fp, p_key, &i_value);
    if(i_ret_fun == APM_FRCONFIG_SUCC)
        st_apm_params.ui_udp_est_timeout = i_value;
    else
    {
        FRLOG_INFO_DETAIL("[%s] key[%s] err", APM_CFG_FILENAME_APM, p_key);
        st_apm_params.ui_udp_est_timeout = 1080; //60*18 sec
    }

    p_key = "apm_relative_timeout";
    i_ret_fun = frconfig_int(fp, p_key, &i_value);
    if(i_ret_fun == APM_FRCONFIG_SUCC)
        st_apm_params.ui_relative_timeout = i_value;
    else
    {
        FRLOG_INFO_DETAIL("[%s] key[%s] err", APM_CFG_FILENAME_APM, p_key);
        st_apm_params.ui_relative_timeout = 60; //60 sec
    }

    p_key = "apm_do_task_cnt";
    i_ret_fun = frconfig_int(fp, p_key, &i_value);
    if(i_ret_fun == APM_FRCONFIG_SUCC)
    {
        if(i_value >=1)
            st_apm_params.ui_do_task_cnt = i_value;
        else
        {
            FRLOG_INFO_DETAIL("[%s] key[%s] err", APM_CFG_FILENAME_APM, p_key);
            st_apm_params.ui_do_task_cnt = 1;
        }
    }
    else
    {
        FRLOG_INFO_DETAIL("[%s] key[%s] err", APM_CFG_FILENAME_APM, p_key);
        st_apm_params.ui_do_task_cnt = 1;
    }

    p_key = "cmd_srv_port";
    i_ret_fun = frconfig_int(fp, p_key, &i_value);
    if(i_ret_fun == APM_FRCONFIG_SUCC)
        st_apm_params.ui_cmd_srv_port = i_value;
    else
    {
        FRLOG_INFO_DETAIL("[%s] key[%s] err", APM_CFG_FILENAME_APM, p_key);
        st_apm_params.ui_cmd_srv_port = 1108; //60 sec
    }

    p_key = "stat_server_hash_size";
    i_ret_fun = frconfig_int(fp, p_key, &i_value);
    if(i_ret_fun == APM_FRCONFIG_SUCC)
        st_apm_params.ui_stat_server_hash_size = i_value;
    else
    {
        FRLOG_INFO_DETAIL("[%s] key[%s] err", APM_CFG_FILENAME_APM, p_key);
        st_apm_params.ui_stat_server_hash_size = 1000;
    }

    p_key = "zmq_connect";
    i_ret_fun = frconfig_string(fp, p_key, &p_value);
    if(p_value )
    {
        snprintf(st_apm_params.arr_zmq_connect, sizeof(st_apm_params.arr_zmq_connect), "%s", p_value);
        frfree(p_value);
        p_value=NULL;
    }
    else
    {
        FRLOG_INFO_DETAIL("[%s] key[%s] err", APM_CFG_FILENAME_APM, p_key);
        snprintf(st_apm_params.arr_zmq_connect, sizeof(st_apm_params.arr_zmq_connect), "tcp://127.0.0.1:1120");//默认1120端口
    }

    p_key = "apmdbg_report_file";
    i_ret_fun = frconfig_string(fp, p_key, &p_value);
    if(p_value )
    {
        snprintf(st_apm_params.arr_apmdbg_report_file, sizeof(st_apm_params.arr_apmdbg_report_file), "%s", p_value);
        frfree(p_value);
        p_value=NULL;
    }
    else
        st_apm_params.arr_apmdbg_report_file[0]='\0';

    p_key = "apmdbg_report_ip";
    i_ret_fun = frconfig_string(fp, p_key, &p_value);
    if(p_value )
    {
        snprintf(st_apm_params.arr_apmdbg_report_ip, sizeof(st_apm_params.arr_apmdbg_report_ip), "%s", p_value);
        frfree(p_value);
        p_value=NULL;
    }
    else
        st_apm_params.arr_apmdbg_report_ip[0]='\0';
    p_key = "apmdbg_report_port";
    i_value = 0;
    i_ret_fun = frconfig_int(fp, p_key, &i_value);
    if(i_ret_fun == APM_FRCONFIG_SUCC && i_value > 0)
        st_apm_params.ui_apmdbg_report_port = i_value;
    else
    {
        FRLOG_INFO_DETAIL("[%s] key[%s] err", APM_CFG_FILENAME_APM, p_key);
        st_apm_params.ui_apmdbg_report_port = 0;
    }
    p_key = "l7_detect_max_times";
    i_value = 0;
    i_ret_fun = frconfig_get_int(APM_CFG_FILENAME_ANALYZER, p_key, &i_value);
    if(i_ret_fun == APM_FRCONFIG_SUCC && i_value > 0)
        st_apm_params.ui_l7_detect_max_times = i_value;
    else
    {
        FRLOG_INFO_DETAIL("[%s] key[%s] err", APM_CFG_FILENAME_ANALYZER, p_key);
        st_apm_params.ui_l7_detect_max_times = 5;
    }
    p_key = "l7_deep_max";
    i_value = 0;
    i_ret_fun = frconfig_get_int(APM_CFG_FILENAME_ANALYZER, p_key, &i_value);
    if(i_ret_fun == APM_FRCONFIG_SUCC && i_value > 0)
        st_apm_params.ui_l7_deep_max = i_value;
    else
    {
        FRLOG_INFO_DETAIL("[%s] key[%s] err", APM_CFG_FILENAME_ANALYZER, p_key);
        st_apm_params.ui_l7_deep_max = 10;
    }
    p_key = "apm_dead_conn_time";
    i_value = 0;
    i_ret_fun = frconfig_get_int(APM_CFG_FILENAME_APM, p_key, &i_value);
    if(i_ret_fun == APM_FRCONFIG_SUCC && i_value > 0)
        st_apm_params.ui_dead_conn_time = i_value;
    else
    {
        FRLOG_INFO_DETAIL("[%s] key[%s] err", APM_CFG_FILENAME_APM, p_key);
        st_apm_params.ui_dead_conn_time = 1800;
    }
    p_key = "apm_session_timeout";
    i_value = 0;
    i_ret_fun = frconfig_get_int(APM_CFG_FILENAME_APM, p_key, &i_value);
    if(i_ret_fun == APM_FRCONFIG_SUCC && i_value > 0)
        st_apm_params.ui_session_timeout = i_value;
    else
    {
        FRLOG_INFO_DETAIL("[%s] key[%s] err", APM_CFG_FILENAME_APM, p_key);
        st_apm_params.ui_session_timeout = 1800;
    }

    frconfig_close(fp);

    if(st_apm_params.ui_tcp_est_timeout < APM_CFG_MIN_TIMEOUT)
        st_apm_params.ui_tcp_est_timeout = APM_CFG_MIN_TIMEOUT;
    if(st_apm_params.ui_tcp_syn_timeout < APM_CFG_MIN_TIMEOUT)
        st_apm_params.ui_tcp_syn_timeout = APM_CFG_MIN_TIMEOUT;
    if(st_apm_params.ui_udp_est_timeout < APM_CFG_MIN_TIMEOUT)
        st_apm_params.ui_udp_est_timeout = APM_CFG_MIN_TIMEOUT;
    if(st_apm_params.ui_relative_timeout < APM_CFG_MIN_TIMEOUT)
        st_apm_params.ui_relative_timeout = APM_CFG_MIN_TIMEOUT;
    if(st_apm_params.ui_dead_conn_time < APM_CFG_MIN_TIMEOUT)
        st_apm_params.ui_dead_conn_time = APM_CFG_MIN_TIMEOUT;
    if(st_apm_params.ui_session_timeout < APM_CFG_MIN_TIMEOUT)
        st_apm_params.ui_session_timeout = APM_CFG_MIN_TIMEOUT;
    return FR_SUCC;
}


/*******************************************************************************
*  FUNC     :  显示配置信息
*  ARGS     :
*  RTN      :
*  NOTE     :
*******************************************************************************/
void frame_params_output(void)
{
    FRLOG_INFO("apm_ct_hash_size=%u", st_apm_params.ui_ct_hash_size);
    FRLOG_INFO("apm_ct_max_cnt=%u", st_apm_params.ui_ct_max_cnt);
    FRLOG_INFO("apm_ct_data_pkg=%u", st_apm_params.ui_ct_data_pkgs);
    FRLOG_INFO("apm_tcp_est_timeout=%u", st_apm_params.ui_tcp_est_timeout);
    FRLOG_INFO("apm_tcp_syn_timeout=%u", st_apm_params.ui_tcp_syn_timeout);
    FRLOG_INFO("apm_udp_est_timeout=%u", st_apm_params.ui_udp_est_timeout);
    FRLOG_INFO("apm_relative_timeout=%u", st_apm_params.ui_relative_timeout);

    FRLOG_INFO("apm_do_task_cnt=%u", st_apm_params.ui_do_task_cnt);

    FRLOG_INFO("cmd_srv_port=%u", st_apm_params.ui_cmd_srv_port);
    FRLOG_INFO("zmq_connect=%s", st_apm_params.arr_zmq_connect);

    FRLOG_INFO("apmdbg_report_file=%s", st_apm_params.arr_apmdbg_report_file);
    FRLOG_INFO("apmdbg_report_ip=%s", st_apm_params.arr_apmdbg_report_ip);
    FRLOG_INFO("apmdbg_report_port=%u", st_apm_params.ui_apmdbg_report_port);
    FRLOG_INFO("l7_detect_max_times=%u", st_apm_params.ui_l7_detect_max_times);
    FRLOG_INFO("l7_deep_max=%u", st_apm_params.ui_l7_deep_max);
    FRLOG_INFO("apm_dead_conn_time=%u", st_apm_params.ui_dead_conn_time);
    FRLOG_INFO("apm_session_timeout=%u", st_apm_params.ui_session_timeout);

    FRLOG_INFO("stat_server_hash_size=%u", st_apm_params.ui_stat_server_hash_size);

    return;
}
